package com.zjh.controller.provider;

import com.mysql.jdbc.StringUtils;
import com.zjh.pojo.Provider;
import com.zjh.pojo.User;
import com.zjh.service.provider.ProviderService;
import com.zjh.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProviderController {

    @Autowired
    ProviderService providerService;

    @RequestMapping("/proQuery")
    public String proQuery(String queryProCode, String queryProName, Model model) {
        if (StringUtils.isNullOrEmpty(queryProCode)) {
            queryProCode = "";
        }
        if (StringUtils.isNullOrEmpty(queryProName)) {
            queryProName = "";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("proName", queryProName);
        map.put("proCode", queryProCode);
        List<Provider> providerList = providerService.getProviderList(map);

        model.addAttribute("providerList", providerList);
        model.addAttribute("queryProCode", queryProCode);
        model.addAttribute("queryProName", queryProName);

        return "providerlist";
    }

    @RequestMapping("/toAddPro")
    public String toAddPro() {
        return "provideradd";
    }

    @PostMapping("/add")
    public String add(Provider provider, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        map.put("proCode", provider.getProCode());
        map.put("proName", provider.getProName());
        map.put("proContact", provider.getProContact());
        map.put("proPhone", provider.getProPhone());
        map.put("proAddress", provider.getProAddress());
        map.put("proFax", provider.getProFax());
        map.put("proDesc", provider.getProDesc());
        map.put("createdBy", ((User) req.getSession().getAttribute(Constants.USER_SESSION)).getId());

        int add = providerService.add(map);

        return "redirect:/proQuery";
    }


}
